﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class AkunEdit : Form
    {
        private Akun parent = null;
        private string acc_name = "";
        private string user_name = "";

        public AkunEdit()
        {
            InitializeComponent();
        }

        public AkunEdit(Akun instance, string data_akun, string data_user, string data_kontak, string data_tipe)
        {
            InitializeComponent();
            parent = instance;

            this.acc_name = data_akun;
            this.user_name = data_user;

            this.akunTextBox.Text = data_akun;
            this.userTextBox.Text = data_user;
            this.contactTextBox.Text = data_kontak;
            this.comboBox1.Text = data_tipe;
        }

        private void AkunEdit_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = Database.getInstance().tes("select * from Account");
            DataRow[] data = dt.Select("acc_name='" + this.acc_name + "' AND user_name='" + this.user_name + "'");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i].ItemArray[0].ToString() == data[0].ItemArray[0].ToString())
                {
                    dt.Rows[i]["acc_name"] = this.akunTextBox.Text;
                    dt.Rows[i]["user_name"] = this.userTextBox.Text;
                    dt.Rows[i]["contact"] = this.contactTextBox.Text;
                    dt.Rows[i]["type"] = this.comboBox1.Text;
                    break;
                }
            }

            Database.getInstance().updateDB(dt);

            int current_row = this.parent.dataGridView1.CurrentCell.RowIndex;

            this.parent.dataGridView1.Rows[current_row].Cells["acc_name"].Value = this.akunTextBox.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["user_name"].Value = this.userTextBox.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["contact"].Value = this.contactTextBox.Text;
            this.parent.dataGridView1.Rows[current_row].Cells["type"].Value = this.comboBox1.Text;
            this.parent.dataGridView1.Refresh();
            this.Close();
        }
    }
}
